package com.bringway.p2p.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.p2p.dao.CzDao;
import com.bringway.p2p.dao.XtdlrzDao;
import com.bringway.p2p.dao.YcxiDao;

/**
 * 
 * @author qjy
 * @version 创建时间：2016年06月14日 上午13:25:42
 * @功能：查询异常信息
 */

@Repository
public class XtdlrzDaoImpl  extends P2PJdbcDao implements XtdlrzDao{
	Logger logger = Logger.getLogger(XtdlrzDaoImpl.class);
	
	
	public List<Map<String, Object>> queryXtdlrzList(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		
		String name=params.optString("name");
		String sjhm=params.optString("sjhm");
		
	
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		
		
		paramMap.put("name", name);
		paramMap.put("sjhm", sjhm);
	
		sql.append(" select ID,USER_NAME,SJHM,LOGIN_IP,date_format(LOGIN_DATE,'%Y-%m-%d %H:%i:%s') LOGIN_DATE,date_format(LOGOUT_DATE,'%Y-%m-%d %H:%i:%s') LOGOUT_DATE from t_zhfw_login_log ");
		sql.append(" where 1=1  ");
		
		if(StringUtils.isNotEmpty(name)){
			sql.append(" AND USER_NAME like "+ "'%"+name+"%'");
			paramMap.put("USER_NAME",name);
		}
		
		if(StringUtils.isNotEmpty(sjhm)){
			sql.append(" AND SJHM like "+ "'%"+sjhm+"%'");
			paramMap.put("SJHM",sjhm);
		}
		
	
		if(0 != limit){
			sql.append(" ORDER BY LOGIN_DATE DESC LIMIT :START,:LIMIT  ");
		}
		logger.info("queryXtdlrzList sql is " + sql.toString());
		logger.info("gettjrCount paramMap is " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForList(
				sql.toString(), paramMap);

	}

	@Override
	public int gettXtdlCount(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		
		String name = params.optString("name");
		String username = params.optString("username");
		String sj = params.optString("sj");
		String ip = params.optString("ip");
		String dltime = params.optString("dltime");
		String tctime = params.optString("tctime");
		
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		paramMap.put("name", name);
		paramMap.put("username", username);
		paramMap.put("sj", sj);
		paramMap.put("dltime", dltime);
		paramMap.put("ip", ip);
		paramMap.put("tctime", tctime);
		sql.append(" SELECT count(*)  ");
		sql.append(" FROM t_zhfw_login_log ");
		sql.append("   where 1=1 ");
		if(StringUtils.isNotEmpty(name)){
			sql.append(" AND USER_NAME like "+ "'%"+name+"%'");
		}
		if(StringUtils.isNotEmpty(username)){
			sql.append(" AND USER_NAME like "+ "'%"+username+"%'");
		}
		if(StringUtils.isNotEmpty(sj)){
			sql.append(" AND SJHM like "+ "'%"+sj+"%'");
		}
		if(StringUtils.isNotEmpty(ip)){
			sql.append(" AND LOGIN_IP like "+ "'%"+ip+"%'");
		}
		if(StringUtils.isNotEmpty(dltime)){
			sql.append(" AND LOGIN_DATE like "+ "'%"+dltime+"%'");
		}
		if(StringUtils.isNotEmpty(tctime)){
			sql.append(" AND LOGOUT_DATE like "+ "'%"+tctime+"%'");
		}
		logger.info("queryXtdlrzList sql is " + sql.toString());
		logger.info("gettjrCount paramMap is " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}



}
